
//通过豆瓣api请求拿到详情内容数据
const rp = require('request-promise-native')
import mogoose from 'mongoose'
const Movie = mogoose.model('Movie')
const Category = mogoose.model('Category')

async function fetchMovie(item) {
    const url = `http://api.douban.com/v2/movie/${item.doubanId}`;
    const res = await rp(url)
    let body
    try {
        body = JSON.parse(res)
    } catch (error) {
        console.log(error)
    }
    return body
}

; (async () => {
    let movies = await Movie.find({
       $or: [
           { summary: {$exists: false}},
           { summary: null},
           { year: { $exists: false}},
           { category: []},
           { title: ''},
           { summary: ''}
       ] 
    })
    console.log(`我是找到的${movies}`)
    movies.map(async movie => {
        try {
            let movieData = await fetchMovie(movie)
            if (movieData) {
                let tags = movieData.tags || [] 
                movie.summary = movieData.summary || ''
                movie.title = movieData.alt_title || movieData.title || ''
                movie.rawTitle = movieData.title || '' 
                if (movieData.attrs) {
                    movie.movieTypes = movieData.attrs.movie_type || []
                    movie.year = movieData.attrs.year[0] || 2500
                    // 这个地方不用foreach循环因为如果使用的话需要传一个异步函数 而异步函数会导致 movie.save方法已经提前执行完了 但是movie.category.push却还未执行
                    for (let i = 0; i < movie.movieTypes.length; i++){
                        let item = movie.movieTypes[i]
                        let cat = await Category.findOne({
                            name: item
                        })
                        if (!cat) {
                            cat = new Category({
                                name: item,
                                movies: [movie._id]
                            })
                        } else {
                            if (cat.movies.indexOf(movie._id) === -1) {
                                cat.movies.push(movie._id)
                            }
                        }
                        await cat.save()
                        if (movie.category.length === 0) {
                            console.log(`执行了 ${cat._id}`)
                            movie.category.push(cat._id)
                        } else {
                            if (movie.category.indexOf(cat._id) === -1) {
                                movie.category.push(cat._id)
                            }
                        }
                    }
                    // 存日期
                    let dates = movieData.attrs.pubdate || []
                    let pubdates = []
                    dates.map(item => {
                        if (item && item.split('(').length > 0) {
                          let parts = item.split('(')
                          let date = parts[0]
                          let country = '未知'
                          
                          if (parts[1]) {
                              country = parts[1].split(')')[0]
                          }
                          pubdates.push({
                              date: new Date(date),
                              country
                          })
                        }
                    })
                    movie.pubdate = pubdates
                }
                if (movie.tags.length === 0) {
                    tags.forEach(tag => {
                        movie.tags.push(tag.name)
                    })
                }
                await movie.save()
            }
        } catch (error) {
            console.log(error)
        }
    })
})()


